<?php
include('../../includes/classes/core.php');
$start  	= $_REQUEST['start'];
$end    	= $_REQUEST['end'];
$count 		= $_REQUEST["count"];
$action 	= $_REQUEST['act'];
$departament= $_REQUEST['departament'];
$type       = $_REQUEST['type'];
$category   = $_REQUEST['category'];
$s_category = $_REQUEST['sub_category'];
$done 		= $_REQUEST['done']%3;
$name 		= $_REQUEST['name'];
$title 		= $_REQUEST['title'];
$text[0] 	= "შემოსული  ზარები კატეგორიების  მიხედვით";
$text[1] 	= "'$departament'- შემოსული ზარები  ზარის ტიპის მიხედვით";
$text[2] 	= "'$departament'- შემოსული ზარები ქვე-კატეგორიების  მიხედვით";
$text[3] 	= "'$departament'- შემოსული  ქვე–კატეგორიის მიხედვით";
$c="3 or incomming_call.call_type_id=0";
if ($type=="ინფორმაცია")  $c=1;
elseif ($type=="პრეტენზია") $c=2;
elseif ($type=="სხვა") $c=3;
//------------------------------------------------query-------------------------------------------
switch ($done){
	case  1:
		$result = mysql_query("SELECT   IF(c_type.`name`!='',c_type.`name`,'შეუვსებელია') as type,
                                		COUNT(*),
                                		CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*) FROM incomming_call
                                			LEFT JOIN category ON incomming_call.category_parent_id = category.id
											LEFT JOIN category as c_type ON incomming_call.category_id = c_type.id
                                			WHERE DATE(`incomming_call`.`call_date`) >= '$start' AND DATE(`incomming_call`.`call_date`) <= '$end' AND category.`name` = '$departament')*100,2),'%')
                                FROM 	incomming_call
                                LEFT JOIN category ON incomming_call.category_parent_id = category.id
								LEFT JOIN category as c_type ON incomming_call.category_id = c_type.id
                                WHERE 	DATE(`incomming_call`.`call_date`) >= '$start' and  DATE(`incomming_call`.`call_date`) <= '$end' AND category.`name` = '$departament'
                                GROUP BY 	c_type.`name`");
		$text[0]=$text[1];
	break;
	case  2:
	    $result = mysql_query("SELECT   IF(sub_cat.`name`!='',sub_cat.`name`,'შეუვსებელია') as type,
                                		COUNT(*),
                                		CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*) FROM incomming_call
                                			LEFT JOIN category ON incomming_call.category_parent_id = category.id
											LEFT JOIN category as c_type ON incomming_call.category_id = c_type.id
											LEFT JOIN category as sub_cat ON incomming_call.sub_category = sub_cat.id
                                			WHERE DATE(`incomming_call`.`call_date`) >= '$start' AND DATE(`incomming_call`.`call_date`) <= '$end' AND category.`name` = '$departament' AND c_type.`name` = '$type')*100,2),'%')
                                FROM 	incomming_call
                                LEFT JOIN category ON incomming_call.category_parent_id = category.id
								LEFT JOIN category as c_type ON incomming_call.category_id = c_type.id
								LEFT JOIN category as sub_cat ON incomming_call.sub_category = sub_cat.id
                                WHERE 	DATE(`incomming_call`.`call_date`) >= '$start' and  DATE(`incomming_call`.`call_date`) <= '$end' AND category.`name` = '$departament' AND c_type.`name` = '$type'
                                GROUP BY 	sub_cat.`name`");
	    $text[0]=$text[2];
	    break;
	default:
		$result = mysql_query("SELECT   IF(category.`name`!='',category.`name`,'შეუვსებელია') as type,
                                		COUNT(*),
                                		CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*) FROM incomming_call
                                		LEFT JOIN category ON incomming_call.category_parent_id = category.id
                                			WHERE DATE(`incomming_call`.`call_date`) >= '$start' AND DATE(`incomming_call`.`call_date`) <= '$end')*100,2),'%')
                                FROM 	incomming_call
                                LEFT JOIN category ON incomming_call.category_parent_id = category.id
                                WHERE 	DATE(`incomming_call`.`call_date`) >= '$start' and  DATE(`incomming_call`.`call_date`) <= '$end'
                                GROUP BY 	category.`name`");

		break;
}
///----------------------------------------------act------------------------------------------
switch ($action) {
	case "get_list":
		$data = array("aaData"	=> array());
		while ( $aRow = mysql_fetch_array( $result ) )
		{	$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				$row[0] = '0';

				$row[$i+1] = $aRow[$i];
			}
			$data['aaData'][] =$row;
		}
		echo json_encode($data); return 0;
		break;
	case 'get_category' :
		$rows = array();
		while($r = mysql_fetch_array($result)) {
			$row[0] = $r[0];
			$row[1] = (float) $r[1];
			$rows['data'][]=$row;
		}
		$rows['text']=$text[0];
		echo json_encode($rows);
		break;
		case 'get_in_page':
		    
		    if($_REQUEST[rid] == 'შეუვსებელია'){
		        $rid = 'AND incomming_call.sub_category = 0';
		    }else{
		        $rid = "AND sub_cat.`name` = '$_REQUEST[rid]'";
		    }
		    
			mysql_query("SET @i = 0;");
			$rResult = mysql_query("SELECT  incomming_call.id,
                                        incomming_call.call_date,
                                        incomming_call.call_phone,
                                        cat.`name`,
                                        cat_type.`name`,
                                        object.`name`,
                                        incomming_call.call_content,
                                        `persons`.`name`
                                FROM    incomming_call
                                LEFT JOIN category as cat ON incomming_call.category_parent_id = cat.id
                                LEFT JOIN category as cat_type ON incomming_call.category_id = cat_type.id
			                    LEFT JOIN category as sub_cat ON incomming_call.sub_category = sub_cat.id
                                LEFT JOIN object ON incomming_call.call_object_id = object.id
			                    JOIN `users` ON incomming_call.user_id = users.id
                                JOIN `persons` ON users.person_id = persons.id
                                WHERE incomming_call.actived = 1 
			                    AND DATE(incomming_call.call_date)>='$start'
			                    AND DATE(incomming_call.call_date)<='$end'
			                    $rid
                			    AND cat_type.`name` = '$_REQUEST[category]'
                                AND cat.`name` = '$_REQUEST[type]'");
					$data = array(
							"aaData"	=> array()
					);
		
					while ( $aRow = mysql_fetch_array( $rResult ) )
					{
					$row = array();
					$row1 = array();
		
					for ( $i = 0 ; $i < $count ; $i++ )
					{
					$row[] = $aRow[$i];
					$a=$aRow;
		
					{
		
					}
					}
						$data['aaData'][] = $row;
					}
					echo json_encode($data); return 0;
					break;
	default :
		echo "Action Is Null!";
		break;

}



?>